//@version=6
indicator("True Strength Index with Crossovers", shorttitle="TSI (Alerts)", format=format.price, precision=4, timeframe="", timeframe_gaps=true)

// Inputs
long = input.int(title="Long Length", defval=25)
short = input.int(title="Short Length", defval=13)
signalLength = input.int(title="Signal Length", defval=13) // Renamed 'signal' to 'signalLength' to avoid conflict with the plot variable
price = close

// Helper function for double EMA smoothing
double_smooth(src, long, short) =>
	fist_smooth = ta.ema(src, long)
	ta.ema(fist_smooth, short)

// Calculate True Strength Index (TSI)
pc = ta.change(price)
double_smoothed_pc = double_smooth(pc, long, short)
double_smoothed_abs_pc = double_smooth(math.abs(pc), long, short)
tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)

// Calculate Signal Line
signal_line = ta.ema(tsi_value, signalLength)

// --- Plotting ---
plot(tsi_value, title="True Strength Index", color=#2962FF)
plot(signal_line, title="Signal", color=#E91E63)
hline(0, title="Zero", color=#787B86)

// --- Crossover Logic and Plotting ---

// Determine crossover events
tsi_cross_up = ta.crossover(tsi_value, signal_line)
tsi_cross_down = ta.crossunder(tsi_value, signal_line)

// Plot dots at crossover points
// Plot green dot when TSI crosses UP above the signal line (Buy Signal)
plotshape(tsi_cross_up, title="Buy Signal", style=shape.circle, location=location.bottom, color=color.new(color.green, 0), size=size.small)

// Plot red dot when TSI crosses DOWN below the signal line (Sell Signal)
plotshape(tsi_cross_down, title="Sell Signal", style=shape.circle, location=location.bottom, color=color.new(color.red, 0), size=size.small)


// --- Alert Conditions ---

// Alert for bullish crossover (TSI crosses above Signal)
alertcondition(tsi_cross_up, title="TSI Buy Crossover", message="TSI crossed above Signal Line - Potential Buy Signal")

// Alert for bearish crossover (TSI crosses below Signal)
alertcondition(tsi_cross_down, title="TSI Sell Crossover", message="TSI crossed below Signal Line - Potential Sell Signal")